package com.xposed.xinhao;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xposed.xinhao.utils.JsonUtils;
import com.xposed.xinhao.utils.LogUtils;
import com.xposed.xinhao.utils.QRCodeUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private static final String RECEIVER_ACTION_NMB_GET_QR_CODE = "com.xposed.hook.newmbank.ACTION_NMB_GET_QR_CODE";
    private static final String RECEIVER_ACTION_NMB_SEND_QR_CODE = "com.xposed.hook.newmbank.ACTION_NMB_SEND_QR_CODE";

    private ImageView imgQrCode;
    private EditText etAmount;
    private Button btnPayUrl;
    private TextView tvPayInfo, tvOrderInfo;

    private MyBroadcastReceiver receiver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVER_ACTION_NMB_SEND_QR_CODE);
        registerReceiver(receiver, intentFilter);

        imgQrCode = (ImageView) findViewById(R.id.img_qr_code);
        etAmount = (EditText) findViewById(R.id.et_amount);
        btnPayUrl = (Button) findViewById(R.id.btn_pay_url);
        tvPayInfo = (TextView) findViewById(R.id.tv_pay_info);
        tvOrderInfo = (TextView) findViewById(R.id.tv_order_info);
        btnPayUrl.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (etAmount.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.setAction(RECEIVER_ACTION_NMB_GET_QR_CODE);
        intent.putExtra("amount", etAmount.getText().toString());
        sendBroadcast(intent);
    }

    /**
     * 广播监听
     */
    private class MyBroadcastReceiver extends BroadcastReceiver {

        @SuppressLint("SetTextI18n")
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == null)
                return;
            if (intent.getAction().equals(RECEIVER_ACTION_NMB_SEND_QR_CODE)) {
                String payInfo = intent.getStringExtra("payInfo");
                if (TextUtils.isEmpty(payInfo)) {
                    tvPayInfo.setText("null");
                } else {
                    tvPayInfo.setText(payInfo);
                    try {
                        JSONObject jsonObject = new JSONObject(payInfo);
                        if (jsonObject.optString("ResponseCode").equals("AAAAAAA")) {
                            imgQrCode.setImageBitmap(QRCodeUtils.createQRCode(jsonObject.optString("promptlyCorpPay_outSeq"), 800));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
